export class MyStoreage {
  storage: Storage
  constructor(storeage: Storage = localStorage) {
    this.storage = storeage
  } // 设置
  setItem(key: string, value: any) {
    if (typeof value === 'string') {
      this.storage.setItem(key, value)
    } else if (
      typeof value === 'bigint' ||
      typeof value === 'number' ||
      typeof value === 'boolean' ||
      typeof value === 'symbol'
    ) {
      this.storage.setItem(key, value.toString())
    } else if (typeof value === 'object' && value !== null) {
      this.storage.setItem(key, JSON.stringify(value))
    }
  }
  // 获取
  getItem(key: string) {
    try {
      // 尝试执行
      return JSON.parse(localStorage.getItem(key) as string)
    } catch {
      // 发生问题
      return localStorage.getItem(key)
    }
  }
  // 移动
  removeItem(key: string) {
    this.storage.removeItem(key)
  }
  // 清空
  clear() {}
}

export const l = new MyStoreage()
export const s = new MyStoreage(sessionStorage)
